/*
 * Projectile.cpp
 *
 *  Created on: Feb 24, 2010
 *      Author: dcrown
 */


#include "Projectile.h"
#include "..\Graphics\DXManager.h"
#include "..\Systems\Logger.h"
#include "..\Systems\BasicWindow.h"

extern DXManager* gpDX;
extern BasicWindow* gpBW;

const int PROJ_IMG_WIDTH = 8;
const int PROJ_IMG_HEIGHT = 8;
const int PROJ_IMG_LEFT_OFFSET = 1;
const int PROJ_IMG_RIGHT_OFFSET = 1;
const int PROJ_IMG_TOP_OFFSET = 4;
const int PROJ_IMG_BOTTOM_OFFSET = 4;

const int MOVE_INC = 10;

void Projectile::Create()
{
	windowWidth = gpBW->GetWidth();

	texture = new Texture;
	texture->Create("resources\\textures\\projectile1.png", "proj", PROJ_IMG_WIDTH, PROJ_IMG_HEIGHT);

	sprite = new Sprite;
	sprite->Create("proj", texture);
	sprite->TranslateOriginal(imageXPos, imageYPos);
	visible = false;
	Logger::getInstance().LOG(Logger::DEBUG, "Projectile Created");
}


void Projectile::Destroy()
{
	texture->Destroy();
	delete texture;
	texture = NULL;

	sprite->Destroy();
	delete sprite;
	sprite = NULL;
}

void Projectile::Render()
{
	if (visible)
	{
		sprite->Draw();
	}
}

void Projectile::Update()
{
	if (visible)
	{
		centerXPos += MOVE_INC;
		AdjustBounds();

		//if the left most point of the projectile is off the screen
		if (imageXPos+PROJ_IMG_LEFT_OFFSET >= windowWidth)
		{
			Deactivate();
		}
		else
		{
			sprite->TranslateOriginal(imageXPos, imageYPos);
			sprite->Update();
		}
	}
}

void Projectile::Activate(int x, int y)
{
	centerXPos = x;
	centerYPos = y;
	visible = true;
}

void Projectile::Deactivate()
{
	//deactivate
	centerXPos = 0;
	centerYPos = 0;
	visible = false;
}

void Projectile::AdjustBounds()
{
	imageXPos = centerXPos - PROJ_IMG_WIDTH/2;
	imageYPos = centerYPos - PROJ_IMG_HEIGHT/2;
}
